package com.study.rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.study.rabbitmq.utils.RabbitMqUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Comsume {
    private static final String EXCHANGE_NAME = "topic";

    public static void main(String[] args) throws IOException {
        Channel channel = RabbitMqUtil.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        String queue = "Q1";
        channel.queueDeclare(queue,false,false,false,null);
        channel.queueBind(queue,EXCHANGE_NAME,"*.orange.*");
        System.out.println("Q1正在等待消息。。。。。");
        DeliverCallback deliverCallback = (consumerTag,message)->{
            System.out.println(new String(message.getEnvelope().getRoutingKey().getBytes(StandardCharsets.UTF_8)));
            System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));
        };
        CancelCallback cancelCallback = (consumerTag)->{
            System.out.println("Q1消息接收中断");
        };
        channel.basicConsume(queue,deliverCallback,cancelCallback);
    }
}
